package services

import (
	"context"
	"github.com/goAdminMicroService/api/proto"
	"github.com/goAdminMicroService/internal/pkg/models"
	"github.com/pkg/errors"
	"go.uber.org/zap"
)

type ProductsService interface {
	// 定义方法，
	Get(c context.Context, ID uint64) (*models.Product, error)
}

type DefaultProductsService struct {
	logger     *zap.Logger
	detailsSvc proto.DetailsClient
}

func NewProductsService(logger *zap.Logger, detailsSvc proto.DetailsClient) ProductsService {
	return &DefaultProductsService{
		logger:     logger,
		detailsSvc: detailsSvc,
	}
}

func (s *DefaultProductsService) Get(c context.Context, productID uint64) (*models.Product, error) {
	var (
		detail *models.Detail
	)

	{
		req := &proto.GetDetailRequest{
			Id: productID,
		}
		// 通过details client 获取
		pd, err := s.detailsSvc.Get(c, req)
		if err != nil {
			return nil, errors.Wrap(err, "get detail error")
		}
		detail = &models.Detail{
			ID:   pd.Id,
			Name: pd.Name,
		}
	}
	return &models.Product{Detail: detail}, nil
}
